<template>
  <div class="app-container">
    <!-- <el-button type="text" @click="dialogVisible = true">添加</el-button> -->
    <!-- <el-card shadow="never"> -->
      <!-- <div slot="header" style="text-align: center;">用户列表</div> -->
      <!-- 店铺店铺列表-->
      <div>
        <el-form>
          <el-row class="rowFormItem">
            <el-col :span="3">
              <el-input v-model="keyword" placeholder="请输入昵称搜索" />
            </el-col>
            <!-- 提交按钮 -->
            <el-col :span="5" class="clear">
              <el-form-item style="text-align: center;width: 150px;margin-left: 10px;">
                <el-button type="primary" size="large" @click="handleSubmit()">检索</el-button>
                <el-button type="warning" size="large" style="margin-left: 8px" @click="handleReset()">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="tableClass">
        <el-table :data="formdata" v-loading="loading" :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          border style="width: 100%;">
          <el-table-column align="center" prop="id" label="序号" />
          <el-table-column align="center" prop="photo_url" label="用户头像">
            <template slot-scope="scope">
              <el-button type="text" size="medium">
                <img style="width: 50%;" :src="scope.row.user_info.photo_url" />
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="user_info.nickname" label="昵称" />
          <el-table-column align="center" prop="user_info.sex_name" label="性别" />
          <el-table-column align="center" prop="phone" label="手机号码">
             <template slot-scope="scope">
                 <div v-if="scope.row.phone ==null">未绑定</div>
                 <div v-else>{{scope.row.phone}}</div>
               </template>
          </el-table-column>
          <el-table-column align="center" prop="user_info.city" label="地区">
           <template slot-scope="scope">
               <div>{{scope.row.user_info.province}}/{{scope.row.user_info.city}}</div>
             </template>
           </el-table-column>
          <el-table-column align="center" prop="reg_time" label="注册时间">
            <template slot-scope="scope">
              <div>{{scope.row.reg_time.substring(0,11)}}</div>
              <div>{{scope.row.reg_time.substring(11,20)}}</div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="invite_user_name" label="邀请人">
            <template slot-scope="scope">
              <div v-if="scope.row.invite_user_name">{{scope.row.invite_user_name}}</div>
              <div v-else>暂无</div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="vip_info" label="会员">
            <template slot-scope="scope">
              <div v-if="scope.row.vip_info!=''">
                <div v-for="(viptype,index) in scope.row.vip_info" :key="index">
<!--                  <div v-if="viptype.type=='num'">-->
<!--                    {{viptype.type_name}}({{viptype.use_num}}/{{viptype.num}})-->
<!--                  </div>-->
                  <div v-if="viptype.type=='time'">
                    {{viptype.type_name}}
                  </div>
                </div>
              </div>
              <div v-else>无</div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="vip_info" label="会员到期">
            <template slot-scope="scope">
             <div v-if="scope.row.vip_info!=''">
                <div v-for="(viptype,index) in scope.row.vip_info" :key="index">
<!--                  <div v-if="viptype.type=='num'">-->
<!--                    无限期-->
<!--                  </div>-->
                  <div v-if="viptype.type=='time'">
                    {{viptype.end_time}}
                  </div>
                </div>
              </div>
              <div v-else>无</div>
            </template>
          </el-table-column>
          <el-table-column label="跟进人" align="center" prop="follow_up_person_name">
            <template slot-scope="scope">
                <el-select style="margin-left: 10px;margin-bottom: 0;" size="mini" filterable
                           v-model="scope.row.follow_up_person_name.id" placeholder="跟进人"
                           @change="setCustomerServiceStaff(scope.row.follow_up_person_name.id,scope.row.id)">
                  <el-option v-for="opt in staffList" :key="opt.id" :value="opt.id" :label="opt.realname" />
                </el-select>
            </template>
          </el-table-column>
         <el-table-column label="操作" align="center">
           <template slot-scope="scope">
             <el-button class="btnAppeal" size="small" type="text" @click="setVip(scope.row.id)">设置会员</el-button>
           </template>
         </el-table-column>
        </el-table>
        <!-- 添加模态款 -->
        <el-dialog style="margin: auto;" :close-on-click-modal="false" :title="title" :visible.sync="modelMsg">
          <div class="msgDiv">
            <table>
              <tr>
                <th>会员类型</th>
                <th>价格</th>
                <th>次数/到期时间</th>
                <th>开关</th>
              </tr>
<!--              <tr>-->
<!--                <td>次数会员</td>-->
<!--                <td>-->
<!--                  <el-input-number class="inputNumer" :disabled="!numCheck"  v-model="vip_arrNum.price" size="small" :precision="2" :step="1"-->
<!--                    :controls="false"></el-input-number>元-->
<!--                </td>-->
<!--                <td>-->
<!--                  最高使用-->
<!--                  <el-input-number class="inputNumer" :disabled="!numCheck"  v-model="vip_arrNum.num" size="small" :precision="0" :step="1"-->
<!--                    :controls="false"></el-input-number>次-->
<!--                </td>-->
<!--                <td>-->
<!--                  <el-switch v-model="numCheck" active-color="#13ce66" inactive-color="#ff4949"></el-switch>-->
<!--                </td>-->
<!--              </tr>-->
              <tr>
                <td>年费会员</td>
                <td>
                  <el-input-number class="inputNumer" :disabled="!timeCheck" v-model="vip_arrTime.price" size="small" :precision="2" :step="0.1"
                    :controls="false"></el-input-number>元
                </td>
                <td>
                  <el-date-picker style="margin: auto;" :disabled="!timeCheck" v-model="vip_arrTime.end_time" type="datetime" placeholder="选择日期时间"
                    align="right" value-format="yyyy-MM-dd HH:mm:ss" :picker-options="pickerOptions">
                  </el-date-picker>
                </td>
                <td>
                  <el-switch v-model="timeCheck" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
                </td>
              </tr>
            </table>
            <el-button type="success" @click="addMsg">{{addORupdate}}</el-button>
          </div>
        </el-dialog>
        <br>
        <!-- 分页 -->
        <div style="text-align: center;">
          <el-pagination background layout="prev, pager, next" @current-change="pageChange" :page-size="pageSize"
            :total="totalusers" />
        </div>
      </div>
    <!-- </el-card> -->
  </div>
</template>

<script>
  export default {
    data() {
      return {
        loading: false,
        keyword: "",
        title: "设置会员",
        addORupdate: "提交",
        modelMsg: false,
        userID: "", //当前选中的会员
        vip_arr: [],
        vip_arrNum: {
          type: "num",
          price: "",
          num: "",
        },
        vip_arrTime: {
          type: "time",
          price: "",
          end_time: ""
        },
        numCheck: false, //选择次数会员
        timeCheck: false, //年费会员
        pickerOptions: {
          shortcuts: [{
              text: '七天会员',
              onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
                picker.$emit('pick', date);
              }
            },
            {
              text: '一个月会员',
              onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() + 3600 * 1000 * 24 * 30);
                picker.$emit('pick', date);
              }
            },
            {
              text: '三个月会员',
              onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() + 3600 * 1000 * 24 * 90);
                picker.$emit('pick', date);
              }
            },
            {
              text: '六个月会员',
              onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() + 3600 * 1000 * 24 * 180);
                picker.$emit('pick', date);
              }
            },
            {
              text: '一年会员',
              onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() + 3600 * 1000 * 24 * 365);
                picker.$emit('pick', date);
              }
            }
          ]
        },
        formdata: [],
        staffList: [],
        totalusers: 0, //总行数
        pageSize: 10, //每页几行
        startpage: 1, //起始页
      }
    },
    mounted() {
      this.getUserList()
      this.getStaffList()

    },
    methods: {

      async setCustomerServiceStaff(staff_id,userId) {
        let params = {
          // customer_clue_id: userId
          staff_id: staff_id,
          user_id:userId
        }
        const res = await this.$api.setCustomerServiceStaff(params)
        if(res.data.level=="success"){
          this.$message.success(res.data.message)
          this.$emit("updateListData")
        } else {
          this.$message.error(res.data.message)
        }
      },
      //获取线索阶段列表数据
      async getStaffList() {
        let params = {
          limit:999,
          page:1,
        }
        const res = await this.$api.getStaffList(params)
        if(res.data.level=="success"){
          let noFollow = {
            id:"",
            realname:"未设置"
          }
          this.staffList.push(noFollow)
          this.staffList = this.staffList.concat(res.data.data)


        } else {
          this.$message.error(res.data.message)
        }
      },
      addMsg() {
        /* 显示加载中。。。 */
        const loadingOne = this.$loading({
          lock: true,
          text: '设置中...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
          this.vip_arr = []
          if (this.numCheck == true) {
            this.vip_arr.push(this.vip_arrNum)
          }
          if (this.timeCheck == true) {
            this.vip_arr.push(this.vip_arrTime)
          }
          let params = {
            user_id: this.userID,
            vip_arr: this.vip_arr
          }
          this.$api.setUserVip(params).then(res=>{
              this.$message({
                type: res.data.level,
                message: res.data.message
              });
              if (res.data.level == "success") {
                this.getUserList()
                loadingOne.close()
                this.modelMsg = false
              } else {
                this.modelMsg = true
                loadingOne.close()
              }
          })

      },
      //设置会员
      setVip(userId) {
        this.modelMsg = true
        this.userID = userId
       this.numCheck=false
       this.vip_arrNum.price=""
       this.vip_arrNum.num=""
       this.timeCheck=false
       this.vip_arrTime.price=""
       this.vip_arrTime.end_time=""

        this.$api.getUserVipList({user_id:userId}).then(res=>{
          res.data.data.forEach(item=>{
              if(item.type=="num"){
                  this.numCheck=true
                  this.vip_arrNum.price=item.price
                  this.vip_arrNum.num=item.num
              }
              if(item.type=="time"){
                  this.timeCheck=true
                  this.vip_arrTime.price=item.price
                  this.vip_arrTime.end_time=item.end_time
              }
          })
        })
      },
      //搜索
      handleSubmit() {
        this.startpage=1
        this.getUserList()
      },
      handleReset() {
        this.keyword = ""
      },
      //获取分页列表
      getUserList() {
        let params = {
          "page": this.startpage,
          "limit": this.pageSize,
          "keyword": this.keyword
        }
        //调用后台提供的接口方法
        this.$api.getUserList(params).then(res => {
          if (res.data.level == "success") {
            this.formdata = res.data.data
            this.totalusers = res.data.page_info.total
            this.loading = false
          } else {
            this.$message.error(res.data.message)
          }


        })
      },
      //分页 pageChange当前页发生改变触发函数
      async pageChange(page) {
        this.startpage = page
        this.loading = true
        //调用获取列表的方法
        this.getUserList()
      },
    }
  }
</script>
<style scoped="scoped">
  /deep/.el-card__header {
    background-color: #409EFF;
    color: whitesmoke;
  }

 /deep/.el-dialog {
   margin-top: 15vh !important;
   width: 45%;
   min-width: 555px;
 }

 /deep/.el-dialog__body {
   padding: 20px 20px 50px;
 }
 table {
   width: 100%;
   font-size: 16px;
   margin-bottom: 40px;
 }

 table th {
   min-width: 40px;
 }

 table tr {
   height: 50px;
 }

 table tr td {
   margin-right: 10px;
 }

 .inputNumer {
   margin: 0 6px;
 }

  .msgDiv {
    width: 80%;
    margin: auto;
    text-align: center;
  }

  .el-input {
    display: flex;
    flex-grow: <2>;
  }
</style>
